package com.xz.courseschedule.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xz.courseschedule.common.vo.BaseVo;
import com.xz.courseschedule.entity.Teacher;
import com.xz.courseschedule.mapper.TeacherMapper;
import com.xz.courseschedule.service.TeacherService;
import com.xz.courseschedule.utils.Result;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.List;

/**
 * @Author: zys
 * @CreateTime: 2025-05-24
 */

@Service
public class TeacherServiceImpl extends ServiceImpl<TeacherMapper, Teacher> implements TeacherService {
    @Override
    public Result getList() {
        List<Teacher> list = super.list();
        return Result.success("成功返回教师列表", list);
    }

    @Override
    public Result getById(Long id) {
        if (id == null) {
            return Result.error(400, "教师ID不能为空");
        }
        Teacher teacher = super.getById(id);
        if (teacher == null) {
            return Result.error(404, "教师不存在");
        }
        return Result.success("成功获取教师信息", teacher);
    }

    @Override
    public Result add(Teacher teacher) {
        if (teacher == null) {
            return Result.error(400, "教师信息不能为空");
        }
        // 检查教师编号是否已存在
        if (teacher.getTeacherNo() != null) {
            LambdaQueryWrapper<Teacher> wrapper = new LambdaQueryWrapper<Teacher>()
                    .eq(Teacher::getTeacherNo, teacher.getTeacherNo());
            Teacher existingTeacher = super.getOne(wrapper);
            if (existingTeacher != null) {
                return Result.error(400, "教师编号已存在");
            }
        }
        boolean result = super.save(teacher);
        if (result) {
            return Result.success("教师添加成功", teacher);
        } else {
            return Result.error(500, "教师添加失败");
        }
    }

    @Override
    public Result update(Teacher teacher) {
        if (teacher == null || teacher.getId() == null) {
            return Result.error(400, "教师信息或ID不能为空");
        }
        // 检查教师是否存在
        Teacher existingTeacher = super.getById(teacher.getId());
        if (existingTeacher == null) {
            return Result.error(404, "教师不存在");
        }
        // 检查教师编号是否与其他教师重复
        if (teacher.getTeacherNo() != null) {
            LambdaQueryWrapper<Teacher> checkWrapper = new LambdaQueryWrapper<Teacher>()
                    .eq(Teacher::getTeacherNo, teacher.getTeacherNo())
                    .ne(Teacher::getId, teacher.getId());
            Teacher duplicateTeacher = super.getOne(checkWrapper);
            if (duplicateTeacher != null) {
                return Result.error(400, "教师编号已存在");
            }
        }
        boolean result = super.updateById(teacher);
        if (result) {
            return Result.success("教师更新成功", teacher);
        } else {
            return Result.error(500, "教师更新失败");
        }
    }

    @Override
    public Result delete(Long id) {
        if (id == null) {
            return Result.error(400, "教师ID不能为空");
        }
        // 物理删除
        boolean result = super.removeById(id);
        if (result) {
            return Result.success("教师删除成功");
        } else {
            return Result.error(500, "教师删除失败");
        }
    }

    @Override
    public Result deleteBatch(Long[] ids) {
        if (ids == null || ids.length == 0) {
            return Result.error(400, "教师ID列表不能为空");
        }
        // 物理删除
        boolean result = super.removeByIds(Arrays.asList(ids));
        if (result) {
            return Result.success("批量删除教师成功");
        } else {
            return Result.error(500, "批量删除教师失败");
        }
    }
}
